﻿#include "ConquestInfo.h"
#include "../MirEnvir/Envir.h"

using namespace Server::MirEnvir;

namespace Server::MirDatabase
{

    ConquestInfo::ConquestInfo()
    {

    }

    ConquestInfo::ConquestInfo(BinaryReader *reader)
    {
        Index = reader->ReadInt32();

        if (Envir::LoadVersion > 73)
        {
            FullMap = reader->ReadBoolean();
        }

        Location = new Point(reader->ReadInt32(), reader->ReadInt32());
        Size = reader->ReadUInt16();
        Name = reader->ReadString();
        MapIndex = reader->ReadInt32();
        PalaceIndex = reader->ReadInt32();
        GuardIndex = reader->ReadInt32();
        GateIndex = reader->ReadInt32();
        WallIndex = reader->ReadInt32();
        SiegeIndex = reader->ReadInt32();

        if (Envir::LoadVersion > 72)
        {
            FlagIndex = reader->ReadInt32();
        }

        counter = reader->ReadInt32();
        for (int i = 0; i < counter; i++)
        {
            ConquestArcherInfo tempVar(reader);
            ConquestGuards.push_back(&tempVar);
        }
        counter = reader->ReadInt32();
        for (int i = 0; i < counter; i++)
        {
            ExtraMaps.push_back(reader->ReadInt32());
        }
        counter = reader->ReadInt32();
        for (int i = 0; i < counter; i++)
        {
            ConquestGateInfo tempVar2(reader);
            ConquestGates.push_back(&tempVar2);
        }
        counter = reader->ReadInt32();
        for (int i = 0; i < counter; i++)
        {
            ConquestWallInfo tempVar3(reader);
            ConquestWalls.push_back(&tempVar3);
        }
        counter = reader->ReadInt32();
        for (int i = 0; i < counter; i++)
        {
            ConquestSiegeInfo tempVar4(reader);
            ConquestSieges.push_back(&tempVar4);
        }

        if (Envir::LoadVersion > 72)
        {
            counter = reader->ReadInt32();
            for (int i = 0; i < counter; i++)
            {
                ConquestFlagInfo tempVar5(reader);
                ConquestFlags.push_back(&tempVar5);
            }
        }

        StartHour = reader->ReadByte();
        WarLength = reader->ReadInt32();
        Type = static_cast<ConquestType>(reader->ReadByte());
        Game = static_cast<ConquestGame>(reader->ReadByte());

        Monday = reader->ReadBoolean();
        Tuesday = reader->ReadBoolean();
        Wednesday = reader->ReadBoolean();
        Thursday = reader->ReadBoolean();
        Friday = reader->ReadBoolean();
        Saturday = reader->ReadBoolean();
        Sunday = reader->ReadBoolean();

        KingLocation = new Point(reader->ReadInt32(), reader->ReadInt32());
        KingSize = reader->ReadUInt16();

        if (Envir::LoadVersion > 74)
        {
            ControlPointIndex = reader->ReadInt32();
            counter = reader->ReadInt32();
            for (int i = 0; i < counter; i++)
            {
                ConquestFlagInfo tempVar6(reader);
                ControlPoints.push_back(&tempVar6);
            }
        }
    }

    void ConquestInfo::Save(BinaryWriter *writer)
    {
        writer->Write(Index);
        writer->Write(FullMap);
        writer->Write(Location->X);
        writer->Write(Location->Y);
        writer->Write(Size);
        writer->Write(Name);
        writer->Write(MapIndex);
        writer->Write(PalaceIndex);
        writer->Write(GuardIndex);
        writer->Write(GateIndex);
        writer->Write(WallIndex);
        writer->Write(SiegeIndex);
        writer->Write(FlagIndex);

        writer->Write(ConquestGuards.size());
        for (int i = 0; i < ConquestGuards.size(); i++)
        {
            ConquestGuards[i]->Save(writer);
        }
        writer->Write(ExtraMaps.size());
        for (int i = 0; i < ExtraMaps.size(); i++)
        {
            writer->Write(ExtraMaps[i]);
        }
        writer->Write(ConquestGates.size());
        for (int i = 0; i < ConquestGates.size(); i++)
        {
            ConquestGates[i]->Save(writer);
        }
        writer->Write(ConquestWalls.size());
        for (int i = 0; i < ConquestWalls.size(); i++)
        {
            ConquestWalls[i]->Save(writer);
        }
        writer->Write(ConquestSieges.size());
        for (int i = 0; i < ConquestSieges.size(); i++)
        {
            ConquestSieges[i]->Save(writer);
        }

        writer->Write(ConquestFlags.size());
        for (int i = 0; i < ConquestFlags.size(); i++)
        {
            ConquestFlags[i]->Save(writer);
        }
        writer->Write(StartHour);
        writer->Write(WarLength);
        writer->Write(static_cast<unsigned char>(Type));
        writer->Write(static_cast<unsigned char>(Game));

        writer->Write(Monday);
        writer->Write(Tuesday);
        writer->Write(Wednesday);
        writer->Write(Thursday);
        writer->Write(Friday);
        writer->Write(Saturday);
        writer->Write(Sunday);

        writer->Write(KingLocation->X);
        writer->Write(KingLocation->Y);
        writer->Write(KingSize);

        writer->Write(ControlPointIndex);
        writer->Write(ControlPoints.size());
        for (int i = 0; i < ControlPoints.size(); i++)
        {
            ControlPoints[i]->Save(writer);
        }

    }

    std::string ConquestInfo::ToString()
    {
        return StringHelper::formatSimple("{0}- {1}", Index, Name);
    }

    ConquestSiegeInfo::ConquestSiegeInfo()
    {

    }

    ConquestSiegeInfo::ConquestSiegeInfo(BinaryReader *reader)
    {
        Index = reader->ReadInt32();
        Location = new Point(reader->ReadInt32(), reader->ReadInt32());
        MobIndex = reader->ReadInt32();
        Name = reader->ReadString();

        if (Envir::LoadVersion <= 84)
        {
            RepairCost = static_cast<int>(reader->ReadUInt32());
        }
        else
        {
            RepairCost = reader->ReadInt32();
        }
    }

    void ConquestSiegeInfo::Save(BinaryWriter *writer)
    {
        writer->Write(Index);
        writer->Write(Location->X);
        writer->Write(Location->Y);
        writer->Write(MobIndex);
        writer->Write(Name);
        writer->Write(RepairCost);
    }

    std::string ConquestSiegeInfo::ToString()
    {
        return StringHelper::formatSimple("{0} - {1} ({2})", Index, Name, Location);
    }

    ConquestWallInfo::ConquestWallInfo()
    {

    }

    ConquestWallInfo::ConquestWallInfo(BinaryReader *reader)
    {
        Index = reader->ReadInt32();
        Location = new Point(reader->ReadInt32(), reader->ReadInt32());
        MobIndex = reader->ReadInt32();
        Name = reader->ReadString();

        if (Envir::LoadVersion <= 84)
        {
            RepairCost = static_cast<int>(reader->ReadUInt32());
        }
        else
        {
            RepairCost = reader->ReadInt32();
        }
    }

    void ConquestWallInfo::Save(BinaryWriter *writer)
    {
        writer->Write(Index);
        writer->Write(Location->X);
        writer->Write(Location->Y);
        writer->Write(MobIndex);
        writer->Write(Name);
        writer->Write(RepairCost);
    }

    std::string ConquestWallInfo::ToString()
    {
        return StringHelper::formatSimple("{0} - {1} ({2})", Index, Name, Location);
    }

    ConquestGateInfo::ConquestGateInfo()
    {

    }

    ConquestGateInfo::ConquestGateInfo(BinaryReader *reader)
    {
        Index = reader->ReadInt32();
        Location = new Point(reader->ReadInt32(), reader->ReadInt32());
        MobIndex = reader->ReadInt32();
        Name = reader->ReadString();

        if (Envir::LoadVersion <= 84)
        {
            RepairCost = static_cast<int>(reader->ReadUInt32());
        }
        else
        {
            RepairCost = reader->ReadInt32();
        }
    }

    void ConquestGateInfo::Save(BinaryWriter *writer)
    {
        writer->Write(Index);
        writer->Write(Location->X);
        writer->Write(Location->Y);
        writer->Write(MobIndex);
        writer->Write(Name);
        writer->Write(RepairCost);
    }

    std::string ConquestGateInfo::ToString()
    {
        return StringHelper::formatSimple("{0} - {1} ({2})", Index, Name, Location);
    }

    ConquestArcherInfo::ConquestArcherInfo()
    {

    }

    ConquestArcherInfo::ConquestArcherInfo(BinaryReader *reader)
    {
        Index = reader->ReadInt32();
        Location = new Point(reader->ReadInt32(), reader->ReadInt32());
        MobIndex = reader->ReadInt32();
        Name = reader->ReadString();
        RepairCost = reader->ReadUInt32();
    }

    void ConquestArcherInfo::Save(BinaryWriter *writer)
    {
        writer->Write(Index);
        writer->Write(Location->X);
        writer->Write(Location->Y);
        writer->Write(MobIndex);
        writer->Write(Name);
        writer->Write(RepairCost);
    }

    std::string ConquestArcherInfo::ToString()
    {
        return StringHelper::formatSimple("{0} - {1} ({2})", Index, Name, Location);
    }

    ConquestFlagInfo::ConquestFlagInfo()
    {

    }

    ConquestFlagInfo::ConquestFlagInfo(BinaryReader *reader)
    {
        Index = reader->ReadInt32();
        Location = new Point(reader->ReadInt32(), reader->ReadInt32());
        Name = reader->ReadString();
        FileName = reader->ReadString();
    }

    void ConquestFlagInfo::Save(BinaryWriter *writer)
    {
        writer->Write(Index);
        writer->Write(Location->X);
        writer->Write(Location->Y);
        writer->Write(Name);
        writer->Write(FileName);
    }

    std::string ConquestFlagInfo::ToString()
    {
        return StringHelper::formatSimple("{0} - {1} ({2})", Index, Name, Location);
    }
}
